package com.zhledu.pi;

import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class PiReduce extends Reducer<Text, LongWritable,Text,DoubleWritable> {
    @Override
    protected void reduce(Text key, Iterable<LongWritable> values, Reducer<Text, LongWritable, Text, DoubleWritable>.Context context) throws IOException, InterruptedException {
        Long pointNum=Long.parseLong(key.toString());
        Long num=0L;
        for(LongWritable point:values){//999:[1,0,1,0,0,1,1]
            num+=point.get();
        }
        double pi=4.0*num/pointNum;
        context.write(key,new DoubleWritable(pi));
    }
}
